#include <iostream>
#include <istream>
#include <fstream>
#include<stdio.h>
#include<stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/socket.h>
#include<string.h>
#include<ctype.h>
#include<arpa/inet.h>
#include "binanceC++server/json.hpp"
#include<netinet/in.h>
#include <string>

using namespace std;
using json = nlohmann::json;
#define SERVER_PORT 9090
#define SERVER_IP "127.0.0.1"
int main()// 命令行参数个数  命令行内容
{
    int sockfd;
    struct sockaddr_in servaddr;
    sockfd=socket(AF_INET, SOCK_STREAM, 0);

    memset(&servaddr, '\0', sizeof(struct sockaddr_in));
    //此处需要标明服务器的IP和端口
    servaddr.sin_family=AF_INET;
    //将IP和端口号进行转化  转化为网络字节序  就是socket函数使用的IP和端口格式
    inet_pton(AF_INET, SERVER_IP, &servaddr.sin_addr);
    servaddr.sin_port = htons(SERVER_PORT);

    connect(sockfd, (struct sockaddr *)&servaddr, sizeof(servaddr));

    string file = "/home/renyu/桌面/testjson.json";

    ifstream in(file);
    json msg = json::parse(in);
    in.close();
    cout << setw(4) << msg << endl;

    string testmsg = msg.dump();
    cout << testmsg.c_str() << endl;
    int len = strlen(testmsg.c_str());
    cout << "send msg len: " << len << endl;
    send(sockfd, testmsg.c_str(), len,0);
    printf("finished.\n");
    close(sockfd);

    return 0;
}